package com.popcorn.member.controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.popcorn.action.Action;
import com.popcorn.action.ActionForward;
import com.popcorn.login.controller.LoginAction;
import com.popcorn.login.controller.LogoutAction;

@WebServlet(name = "MemberController",urlPatterns = "*.ho")
public class MemberController extends javax.servlet.http.HttpServlet
implements javax.servlet.Servlet {
		/**
		* 
		*/
		private static final long serialVersionUID = 1L;
		
		protected void doProcess(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html; charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		
		String RequestURI = request.getRequestURI(); 
		String contextPath = request.getContextPath();   
		String command = RequestURI.substring(contextPath.length());
		
		System.out.println("========================MemberController======================== ");
		System.out.println("RequestURI : "+ RequestURI);
		System.out.println("contextPath : "+ contextPath);
		System.out.println("command : "+ command);
		
		ActionForward forward = null;
		Action action = null;
		
		if (command.equals("/member/member.ho")) { // 삽입
		   System.out.println("/member/member.ho");
		   forward = new ActionForward();
		   action = new MemberInsertAction();
		   forward.setRedirect(false);
		   forward.setPath("/main/main.jsp");
		} else if (command.equals("/member/id_check.ho")) { // id 체크
		   System.out.println("/member/id_check.ho");
		   forward = new ActionForward();
		   action = new MemberCheckAction_ID();
		   forward.setRedirect(false);
		   forward.setPath("/main/main.jsp");
		} else if (command.equals("/member/Nick_check.ho")) { // nickname 체크
		   System.out.println("/member/Nick_check.ho");
		   forward = new ActionForward();
		   action = new MemberCheckAction_Nick();
		   forward.setRedirect(false);
		   forward.setPath("/main/main.jsp");
		} else if (command.equals("/member/update_member.ho")) { // 회원 수정 View
			   System.out.println("/member/update_member.ho");
			   forward = new ActionForward();
			   action = new MemberUpdateViewAction();
			   forward.setRedirect(false);
			   forward.setPath("/login/login.jsp");
		} else if (command.equals("/member/update.ho")) { // 회원 수정
			   System.out.println("/member/update.ho");
			   forward = new ActionForward();
			   action = new MemberUpdateAction();
			   forward.setRedirect(false);
			   forward.setPath("/login/login.jsp");
		}
		
		try{
		   forward = action.execute(request, response);
		}catch(Exception e){
		   e.printStackTrace();
		}
		 
		if(forward != null){
			   if(forward.isRedirect()){
				   response.sendRedirect(forward.getPath());
			   }else{
				   RequestDispatcher dispatcher=
					   request.getRequestDispatcher(forward.getPath());
				   dispatcher.forward(request, response);
			   }
			}
		}

	protected void doGet(HttpServletRequest request,
		         HttpServletResponse response) throws ServletException, IOException {
		      doProcess(request, response);
	}

	protected void doPost(HttpServletRequest request,
		         HttpServletResponse response) throws ServletException, IOException {
		      doProcess(request, response);
	}
}